import { h } from 'vue'

export function useDialog(options) {
  const { type, title, content, confirm, cancel, icon, negative, closable, positive } = options
  const dialog = $dialog.create({
    type: type || 'info',
    title: title || '提示',
    content: () => h('div', { class: 'pb-6' }, { default: () => content || '' }),
    closeOnEsc: false,
    showIcon: icon || false,
    maskClosable: false,
    loading: false,
    closable: closable === false ? closable : true,
    positiveText: positive || '确定',
    positiveButtonProps: {
      type: type || 'info',
      size: 'large',
      autoFocus: true,
    },
    negativeText: negative === '' ? negative : '取消',
    negativeButtonProps: {
      type: type || 'info',
      size: 'large',
    },
    onPositiveClick: async () => {
      try {
        dialog.negativeButtonProps.disabled = true
        dialog.loading = true
        dialog.closable = false
        await confirm()
      } finally {
        dialog.negativeButtonProps.disabled = false
        dialog.loading = false
        dialog.closable = true
      }
    },
    onNegativeClick: async () => {
      await cancel()
    },
  })
  return dialog
}
